<!-- 库存迁移：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
        <div class="flex">
          <h1 class="f16">库存迁移</h1>
          <JDialog :tableHeader="tableHeader" :width="73" :currentTableRow="currentTableRow" @confirm="confirm" :unEdit="false" :unSub="true" />
        </div>
        <Table
          :tableHeader="tableHeader"
          @selectList="selectTableList"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>

<script lang="ts" setup name="InventoryMigration">
import JMessage from '@/utils/JMessage'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { watch } from 'vue'
import { onMounted } from 'vue'
import { ref } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入物料编号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入物料名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入物料颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入出入库单号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectP: '请选择入库状态',
    selectValue: '',
    selectLabel: '入库状态',
    prop: 'orderStatus',
    selectOptions: [
      {
        optionLabel: '已作废',
        optionValue: '-1',
        optionKey: '-1'
      },
      {
        optionLabel: '待入库',
        optionValue: '0',
        optionKey: '0'
      },
      {
        optionLabel: '已入库',
        optionValue: '1',
        optionKey: '1'
      }
    ]
  },
  {
    type: 'selectJV2',
    label: '请选择仓库',
    prop: 'stkId',
    options: []
  },
  {
    type: 'select',
    selectP: '请选择订单类型',
    selectValue: '',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  },
  {
    type: 'button',
    checkboxLabel: '打勾数据移库'
  },
  {
    type: 'button',
    checkboxLabel: '打勾移库单作废'
  }
]) as any
const tableHeader = ref([
  {
    label: '物料编号',
    prop: 'id'
  },
  {
    label: '仓库名称',
    prop: 'warehouseName'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '打勾数据移库') {
    // 移库批处理
    if (selectedLists.value.length === 0) {
      JMessage.warning('请选择需要移库的数据')
    } else {
      const res = await service.post<any, any>('/store/mtl/move', selectedLists.value)
      if (res.code === 200) {
        JMessage.success('移库批处理成功')
        getTableList()
      } else {
        JMessage.error(res.msg)
      }
    }
  }
  if (content === '打勾移库单作废') {
    // 打勾移库单作废
    if (selectedLists.value.length === 0) {
      JMessage.warning('请选择需要作废的移库单')
      return
    }
    const res = await service.post<any, any>(`/store/mtl/move/abandon?flag=${1}&idList=${selectedLists.value.map((item: any) => item.id)}`)
    if (res.code === 200) {
      JMessage.success('移库单作废成功')
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/store/mtl/move/list', {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    mtl: {
      mtlNo: formList.value.find((item: any) => item.inputP === '物料编号')?.inputValue,
      mtlName: formList.value.find((item: any) => item.inputP === '物料名称')?.inputValue,
      mtlColor: formList.value.find((item: any) => item.inputP === '物料颜色')?.inputValue
    },
    orderStatus: formList.value.find((item: any) => item.selectP === '请选择入库状态')?.selectValue,
    stkId: formList.value.find((item: any) => item.label === '请选择仓库')?.value,
    orderType: formList.value.find((item: any) => item.selectP === '请选择订单类型')?.selectValue,
    storeOrderNo: formList.value.find((item: any) => item.inputP === '请输入出入库单号')?.inputValue
  })
  isLoading.value = false
  tableData.value = tableDataList.data.records
  total.value = tableDataList.data.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
}
const selectedLists = ref([]) as any
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
// Dialog确认添加/修改按钮
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 打印出obj的值
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await service.put(`/store/mtl/move/add`, {
      ...obj
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.msg)
    } else {
      JMessage.error(res.msg)
    }
  } else if (title === '修改') {
    const res = (await service.put(`/store/mtl/move/edit`, {
      ...obj
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.msg)
    } else {
      JMessage.error(res.msg)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList()
}

const getSelectList = async () => {
  // 获取订单类型
  const res = await service.get<any, any>(`/store/OrderType/back?orderType=${1}`)
  if (res.code === 200) {
    formList.value.find((item: any) => item.selectP === '请选择订单类型').selectOptions = res.data.map((item: any) => ({
      optionLabel: item.typeName,
      optionValue: item.id,
      optionKey: item.id
    }))
  }
  // 获取仓库下拉框
  const res2 = (await service.get(`/store/warehouse/back`)) as any
  formList.value.find((item: any) => item.label === '请选择仓库').options = res2.data.map((item: any) => ({
    value: item.stkId,
    label: item.warehouseName
  }))
}
onMounted(() => {
  getSelectList()
})
</script>
